﻿Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports DotNetNuke.Services.Localization

Namespace Aricie.DNN.Modules.LuceneSearch.UI.Controls

    Public Class ResultsPagerHelper

        ''' <summary>
        ''' Calcule Nb Pages
        ''' </summary>
        ''' <param name="nbResults"></param>
        ''' <param name="doubleDefaultPageSize"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function CalculatePagesCount(ByVal nbResults As Integer, ByVal doubleDefaultPageSize As Double) As Integer
            Dim doubleNbResults As Double = Convert.ToDouble(nbResults)
            Return CInt(IIf(Int(doubleNbResults / doubleDefaultPageSize) = doubleNbResults / doubleDefaultPageSize, doubleNbResults / doubleDefaultPageSize, Int(doubleNbResults / doubleDefaultPageSize) + 1))
        End Function

        ''' <summary>
        ''' Calcule Nb Pages
        ''' </summary>
        ''' <param name="nbResults">Results Count</param>
        ''' <param name="allowCustomPageSize">Allow Custom Page Size</param>
        ''' <param name="customPageSize">Custom Page Size Value</param>
        ''' <param name="defaultPageSize">Default Page Size Value</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CalculatePagesCount(ByVal nbResults As Integer, ByVal allowCustomPageSize As Boolean, ByVal customPageSize As Integer, ByVal defaultPageSize As Integer) As Integer
            Dim doubleDefaultPageSize As Double
            If allowCustomPageSize Then
                If Convert.ToDouble(customPageSize) < 1.0 Then
                    doubleDefaultPageSize = Convert.ToDouble(defaultPageSize)
                Else
                    doubleDefaultPageSize = Convert.ToDouble(customPageSize)
                End If

            Else
                doubleDefaultPageSize = Convert.ToDouble(defaultPageSize)
            End If

            Return ResultsPagerHelper.CalculatePagesCount(nbResults, doubleDefaultPageSize)
        End Function

    End Class
End Namespace
